import java.util.Arrays;

class Solution {
    public int longestCommonSubsequence(String text1, String text2) {
        //最长公共子序列
        int m = text1.length();
        int n = text2.length();
        text1 = " " + text1;
        text2 = " " + text2;
        //创建dp表
        int[][] dp = new int[m+1][n+1];
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(text1.charAt(i) == text2.charAt(j)){
                  dp[i][j] = dp[i-1][j-1] + 1;
                }else{
                    dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        return dp[m][n];
    }
    //不相交的线
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int m = nums1.length;
        int n = nums2.length;
        int[][] dp = new int[m + 1][n + 1];
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(nums1[i - 1] == nums2[j - 1]) dp[i][j] = dp[i-1][j-1] + 1;
                else dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]);
            }
        }
        return dp[m][n];
    }
    //不同的子序列
    public int numDistinct(String s, String t) {
        int n = s.length();
        int m = t.length();
        //创建dp表
        int[][] dp = new int[m+1][n+1];
        s = " " + s;
        t = " " + t;
        //初始化
        for(int i = 0; i <= n; i++){
            dp[0][i] = 1;
        }
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] += dp[i][j - 1];
                if(t.charAt(i) == s.charAt(j)){
                    dp[i][j] += dp[i - 1][j - 1];
                }

            }
        }
        return dp[m][n];
    }
    //通配符匹配
    public boolean isMatch(String s, String p) {
        int m = s.length();
        int n = p.length();
        s = " " + s;
        p = " " + p;
        //创建dp表
        boolean[][] dp = new boolean[m+1][n+1];
        //初始化
        dp[0][0] = true;
        for(int j = 1; j <= n; j++){
            if(p.charAt(j) == '*') dp[0][j] = true;
            else break;
        }
        //填表
        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(p.charAt(j) == '?'){
                    dp[i][j] = dp[i-1][j-1];
                }else if(p.charAt(j) == '*'){
                    dp[i][j] = dp[i][j-1] || dp[i-1][j];
                }else if(s.charAt(i) == p.charAt(j)){
                    dp[i][j] = dp[i-1][j-1];
                }
            }
        }
        //返回
        return dp[m][n];
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums1 = {1, 4, 2};
        int[] nums2 = {1, 2, 4};
        solution.maxUncrossedLines(nums1, nums2);
        String s = "babgbag";
        String t = "bag";
        solution.numDistinct(s, t);

    }
}